#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>


typedef struct QNode
{
	struct QNode* next;
	int date;
}QNode;

typedef struct Queue
{
	QNode* front;
	QNode* tail;
}Queue;


void QueueInit(Queue* ps)
{
	assert(ps);
	ps->front = ps->tail = NULL;
}

void QueueDestory(Queue* ps)
{
	assert(ps);
	while (ps->front != NULL)
	{
		QNode* next = ps->front->next;
		free(ps->front);
		ps->front = next;
	}

}


void QueuePush(Queue* ps, int e)
{
	assert(ps);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	else
	{
		newnode->date = e;
		newnode->next = NULL;

		if (ps->tail == NULL)
		{
			ps->front=ps->tail = newnode;
		}
		else
		{
			ps->tail->next = newnode;
			ps->tail = newnode;//1
		}
	}
}

void QueuePop(Queue* ps)
{
	assert(ps);
	assert(ps->front);
	if (ps->front->next == NULL)
	{
		free(ps->front);
		ps->front = ps->tail = NULL;
	}
	else
	{
		QNode* next = ps->front->next;
		free(ps->front);
		ps->front = next;
	}
}


int QueueTail(Queue* ps)
{
	assert(ps);
	assert(ps->front);
	return ps->tail->date;
}

int QueueFront(Queue* ps)
{
	assert(ps);
	assert(ps->front);
	return ps->front->date;
}

int QueueSize(Queue* ps)
{
	assert(ps);

	int size = 0;
	QNode* cur = ps->front;
	while (cur)
	{
		cur = cur->next;
		size++;
	}
	return size;

}

int QueueIsEmpty(Queue* ps)
{
	assert(ps);
	return ps->front ==ps->tail == NULL;
}

int main()
{
	Queue q;
	QueuePush(&q, 1);
	printf("%d ", QueueFront(&q));
	QueuePush(&q, 2);

	printf("%d ", QueueFront(&q));
	QueuePop(&q);
	printf("%d ", QueueFront(&q));

	QueuePush(&q, 3);
	QueuePush(&q, 4);

	while (!QueueIsEmpty(&q))
	{
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}

	QueueDestory(&q);
}


//void TestQueue()
//{
//	Queue q;
//	QueueInit(&q);
//	QueuePush(&q, 1);
//	QueuePush(&q, 2);
//	printf("%d ", QueueFront(&q));
//	QueuePop(&q);
//	printf("%d ", QueueFront(&q));
//	QueuePop(&q);
//
//	QueuePush(&q, 3);
//	QueuePush(&q, 4);
//
//	while (!QueueEmpty(&q))
//	{
//		printf("%d ", QueueFront(&q));
//		QueuePop(&q);
//	}
//	printf("\n");
//
//	QueueDestory(&q);
//}